package com.northwest_man.refactor_tool;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NewOldStringCompare {
    public static void main() {
        final List<String> stringFileList = new ArrayList<>();
        FileUtil.recursionFolder(new File(Constants.pastedParentResFolder), stringFileList, new Handler() {
            @Override
            public Object doHandle(File curFile, List allFileAbsolutePath) {
                if ("strings.xml".equals(curFile.getName())) {
                    allFileAbsolutePath.add(curFile.getAbsolutePath());
                }
                return null;
            }
        });
        for (final String fileAPath : stringFileList) {
            final StringBuilder fileContent = FileUtil.getFileContent(fileAPath);
            if (fileContent == null) {
                System.out.println("无法获取该文件内容" + fileAPath);
                continue;
            }
            // <string\s+name\s*=\s*"([^"]+)"\s*>([\s\S]+?)</string\s*>
            final Pattern pattern = Pattern.compile("<string\\s+name\\s*=\\s*\"([^\"]+)\"\\s*>([\\s\\S]+?)</string\\s*>");
            final Matcher matcher = pattern.matcher(fileContent);
            final List<StringKV> stringKVList = new ArrayList<>();
            while (matcher.find()) {
                stringKVList.add(new StringKV(matcher.group(1), matcher.group(2)));
            }
            final String fromFileAPath = Constants.copyParentResFolder + fileAPath.substring(Constants.pastedParentResFolder.length());
            final StringBuilder fromFileContent = FileUtil.getFileContent(fromFileAPath);
            if (fromFileContent == null) {
                System.out.println("无法获取源文件内容" + fromFileAPath);
                continue;
            }
            final Matcher matcher2 = pattern.matcher(fromFileContent);

            while (matcher2.find()) {
                boolean found = false;
                for (int i = stringKVList.size() - 1; i >= 0; i--) {
                    final StringKV stringKV = stringKVList.get(i);
                    if (matcher2.group(1).equals(stringKV.key)) {
                        found = true;
                        if (matcher2.group(2).equals(stringKV.value)) {
                        } else {
                            System.out.println("---------------------------------值不同开始---------------------------------");
                            System.out.println(fileAPath);
                            System.out.println("key: " + stringKV.key);
                            System.out.println("old: " + matcher2.group(2));
                            System.out.println("new: " + stringKV.value);
                            System.out.println("---------------------------------值不同结束---------------------------------");
                        }
                        stringKVList.remove(i);
                        break;
                    }
                }
                if (!found) {
                    System.out.println("---------------------------------副本缺失值---------------------------------");
                    System.out.println(fileAPath);
                    System.out.println("key: " + matcher2.group(1));
                    System.out.println("old: " + matcher2.group(2));
                    System.out.println("---------------------------------副本缺失值---------------------------------");
                }
            }
            if (stringKVList.isEmpty()) {

            } else {
                System.out.println("---------------------------------源文件中找不到该key---------------------------------");
                for (final StringKV stringKV : stringKVList) {
                    System.out.println(stringKV.key);
                    System.out.println(stringKV.value);
                }
                System.out.println("---------------------------------源文件中找不到该key---------------------------------");
            }
        }
    }

}
